package com.wosai.register.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.wosai.register.model.Employee;
import com.wosai.register.model.UserInfo;
import com.wosai.register.repository.EmployeeRepository;
import com.wosai.register.repository.UserRepository;

@Service
public class MyUserDetailsService implements UserDetailsService {
    private static final Log logger = LogFactory.getLog(MyUserDetailsService.class);

    private final UserRepository userRepository;
    
    @Autowired
    public MyUserDetailsService(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }
    
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException 
    {
        try {
            UserInfo user = this.userRepository.findByUsername(username);
            return user;
        }catch(Exception e){
            try {
                UserInfo user = this.userRepository.findByMobile(username);
                return user;
            }catch(Exception ex){
                throw new UsernameNotFoundException(username + " not found.", e);
            }
        }
    }

}
